package com.webchat.connect.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.AccountRelationTypeEnum;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.vo.request.AccountRelationQueryDTO;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import com.webchat.rmi.user.UserServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class AccountService {

    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 批量查询账号详情
     *
     * @param userIds
     * @return
     */
    public Map<String, UserBaseResponseInfoVO> batchGet(Set<String> userIds) {
        APIResponseBean<Map<String, UserBaseResponseInfoVO>> responseBean = userServiceClient.batchGet(userIds);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        // TODO 建议做都低策略（两级缓存 local cache + redis）
        return null;
    }


    /**
     * 获取账号详情，底层走Redis查询
     * @param account
     * @return
     */
    public UserBaseResponseInfoVO accountInfo(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        APIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.userInfo(account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        log.error("RPC Invoke Error ====> UserServiceClient.userInfo error, account:{}, responseBean:{}",
                   account, JsonUtil.toJsonString(responseBean));
        return null;
    }

    /**
     * 查询所有群成员
     * @param account
     * @return
     */
    public Set<String> getGroupUserIds(String account) {

        return getAllSubscriberByAccount(account, AccountRelationTypeEnum.USER_GROUP);
    }

    /**
     * 查询公众号所有订阅用户
     *
     * @param account
     * @return
     */
    public Set<String> getOfficialUserIds(String account) {

        return getAllSubscriberByAccount(account, AccountRelationTypeEnum.USER_OFFICIAL);
    }

    /**
     * 获取群组下的群成员用户id集合
     *
     * @return
     */
    public Set<String> getAllSubscriberByAccount(String account, AccountRelationTypeEnum accountRelationType) {
        /**
         * TODO 建议: 对数据做降级
         */
        APIResponseBean<Set<String>> responseBean =
                userServiceClient.getAllSubscriberByAccount(accountRelationType.getType(), account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }

    /**
     * 判断userAccount是否订阅account
     * @param userAccount
     * @param account
     * @param accountRelationType
     * @return
     */
    public boolean isSubscribe(String userAccount, String account, AccountRelationTypeEnum accountRelationType) {
        APIResponseBean<Boolean> responseBean =
                userServiceClient.isSubscribe(accountRelationType.getType(), userAccount, account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        return false;
    }


    /**
     * 批量校验用户是否订阅某账号
     *
     * @param userAccounts
     * @param account
     * @param accountRelationType
     * @return
     */
    public Map<String, Boolean> batchIsSubscribe(List<String> userAccounts, String account,
                                                 AccountRelationTypeEnum accountRelationType) {

        AccountRelationQueryDTO queryParam = AccountRelationQueryDTO.builder()
                                                                    .relationType(accountRelationType.getType())
                                                                    .sourceAccountIds(userAccounts)
                                                                    .targetAccountId(account)
                                                                    .build();
        APIResponseBean<Map<String, Boolean>> responseBean = userServiceClient.batchIsSubscribe(queryParam);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyMap();
    }
}
